#!/usr/bin/env python
import qb_database
import qb
import time
q = qb_database.QubeDatabase()
host_big_dict = q.host(query_field='name,resources,groups,description')


def make_txt(content,filename):
    path = '/show/WDN/misc/dept/lgt/RenderFarm/archive/Memory_check/'
    with open(path + filename + '.txt', 'a') as outfile:
        outfile.writelines(content)
time_check = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
make_txt(time_check+'\n', 'Call IT fix')
make_txt(time_check+'\n', 'G128')
make_txt(time_check+'\n', 'G96')
for item in host_big_dict:
    if item['description'] == '':
        if 'G128' in item['groups']:
            resources_list = map(str,item['resources'].split(','))
            str_memory = map(str,resources_list[2].split('/'))
            if int(str_memory[1])/1000 < 128:
                make_txt(item['name']+'    '+str_memory[1]+'\n','G128')
                if int(str_memory[1])/1000 < 96:
                    # print qb.workerlock(item['name'], lock=True, purge=True)
                    make_txt(item['name'] + '    ' + str_memory[1] + '\n', 'Call IT fix')

        if 'G96' in item['groups']:
            resources_list = map(str, item['resources'].split(','))
            str_memory = map(str, resources_list[2].split('/'))
            if int(str_memory[1]) / 1000 < 96:
                make_txt(item['name'] + '    ' + str_memory[1] + '\n', 'G96')
                if int(str_memory[1])/1000 < 50:
                    make_txt(item['name'] + '    ' + str_memory[1] + '\n', 'Call IT fix')
                    print qb.workerlock(name=item['name'], lock=True, purge=True)